package com.fact.actions;

import java.util.ArrayList;
import java.util.Map;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.fact.dto.UserDTO;
import com.fact.models.UserService;
import com.fact.utils.AppConstant;

public class User extends BaseAction implements SessionAware{
	
	private static final long serialVersionUID = -7546229642641652565L;
	private Map<String,Object> session;
	private ArrayList<UserDTO> userList;
	
	private String page=AppConstant.USER;
	private String pageTitle="FMtool - User List";

	UserDTO user=new UserDTO();
	UserService userService=new UserService();
	private String opType;

	public String userList()
	{
		if(opType==null || (!opType.equalsIgnoreCase("save") && !opType.equalsIgnoreCase("delete") && !opType.equalsIgnoreCase("edit")))
			this.session.put("operation_message", null);
		
		System.out.println("In userList Method.");
		userList= userService.getUserList();
		
		return SUCCESS;
	}
	public String newUserForm()
	{
		
		return SUCCESS;
	}
	public String editUser()
	{
		user.setLastUpdatedBy(((UserDTO)session.get("user")).getUserId());
		
		if(userService.editUser(user)==false)
		{
			addActionMessage("Error in User Edit Operation.");
			return INPUT;
			
		}
		else
		{
			this.session.put("operation_message","User with User Name '"+ user.getUserName()+"' has successfully been updated.");
			return  SUCCESS;
		}
	
	}
	public String saveUser()
	{ 

		
		if(userService.validateUserId(user.getUserId())==false)
			addActionMessage("Userid Already Exist.");
		
		if(hasActionMessages())
			return  INPUT;

		user.setCreatedBy(((UserDTO)session.get("user")).getUserId());
		if(userService.saveUser(user)==false)
				{
					addActionMessage("Error in User Save Operation.");
					return INPUT;
				}
		else
				{
					this.session.put("operation_message", user.getUserName()+" has successfully been added in User List.");
					return  SUCCESS;
				}

	}
	
	public String deleteUser()
	{

		
		if(userService.deleteUser(user.getUserId())==false)
		{
			addActionMessage("Error in User Delete Operation.");
			return INPUT;
			
		}
		else
		{
			this.session.put("operation_message","User with User Name '"+ user.getUserName()+"' has successfully been deleted from User List.");
			return  SUCCESS;
		}
	
	}
	
	public void setSession(Map<String, Object> session) 
	{
		this.session = session;
	}
	public ServletContext getServletContext()
	{
		return ServletActionContext.getServletContext();
	}
	public ArrayList<UserDTO> getUserList() {
		return userList;
	}
	public void setUserList(ArrayList<UserDTO> userList) {
		this.userList = userList;
	}
	public String getPage() {
		return page;
	}
	public void setPage(String page) {
		this.page = page;
	}
	public String getPageTitle() {
		return pageTitle;
	}
	public void setPageTitle(String pageTitle) {
		this.pageTitle = pageTitle;
	}
	public UserDTO getUser() {
		return user;
	}
	public void setUser(UserDTO user) {
		this.user = user;
	}
	public String getOpType() {
		return opType;
	}
	public void setOpType(String opType) {
		this.opType = opType;
	}
	

}
